Conditions | 20 |
Total Lines | 67 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like proportions.js ➔ setData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | define(['d3-interpolate', 'snabbdom', 'utils/version', 'filters/genericnode', 'helper'], |
||
79 | self.setData = function setData(data) { |
||
80 | var onlineNodes = data.nodes.online; |
||
81 | var nodes = onlineNodes.concat(data.nodes.lost); |
||
82 | time = data.timestamp; |
||
83 | |||
84 | function hostnameOfNodeID(nodeid) { |
||
85 | var gateway = data.nodeDict[nodeid]; |
||
86 | if (gateway) { |
||
87 | return gateway.hostname; |
||
88 | } |
||
89 | return null; |
||
90 | } |
||
91 | |||
92 | var gatewayDict = count(nodes, ['gateway'], hostnameOfNodeID); |
||
93 | var gateway6Dict = count(nodes, ['gateway6'], hostnameOfNodeID); |
||
94 | |||
95 | var statusDict = count(nodes, ['is_online'], function (d) { |
||
96 | return d ? 'online' : 'offline'; |
||
97 | }); |
||
98 | var fwDict = count(nodes, ['firmware', 'release']); |
||
99 | var hwDict = count(nodes, ['model']); |
||
100 | var geoDict = count(nodes, ['location'], function (d) { |
||
101 | return d && d.longitude && d.latitude ? _.t('yes') : _.t('no'); |
||
102 | }); |
||
103 | |||
104 | var autoDict = count(nodes, ['autoupdater'], function (d) { |
||
105 | if (d.enabled) { |
||
106 | return d.branch; |
||
107 | } |
||
108 | return _.t('node.deactivated'); |
||
109 | }); |
||
110 | |||
111 | var domainDict = count(nodes, ['domain'], function (d) { |
||
112 | if (config.domainNames) { |
||
113 | config.domainNames.some(function (t) { |
||
114 | if (d === t.domain) { |
||
|
|||
115 | d = t.name; |
||
116 | return true; |
||
117 | } |
||
118 | }); |
||
119 | } |
||
120 | return d; |
||
121 | }); |
||
122 | |||
123 | statusTable = fillTable('node.status', statusTable, statusDict.sort(function (a, b) { |
||
124 | return b[1] - a[1]; |
||
125 | })); |
||
126 | fwTable = fillTable('node.firmware', fwTable, fwDict.sort(versionCompare)); |
||
127 | hwTable = fillTable('node.hardware', hwTable, hwDict.sort(function (a, b) { |
||
128 | return b[1] - a[1]; |
||
129 | })); |
||
130 | geoTable = fillTable('node.visible', geoTable, geoDict.sort(function (a, b) { |
||
131 | return b[1] - a[1]; |
||
132 | })); |
||
133 | autoTable = fillTable('node.update', autoTable, autoDict.sort(function (a, b) { |
||
134 | return b[1] - a[1]; |
||
135 | })); |
||
136 | gatewayTable = fillTable('node.selectedGatewayIPv4', gatewayTable, gatewayDict.sort(function (a, b) { |
||
137 | return b[1] - a[1]; |
||
138 | })); |
||
139 | gateway6Table = fillTable('node.selectedGatewayIPv6', gateway6Table, gateway6Dict.sort(function (a, b) { |
||
140 | return b[1] - a[1]; |
||
141 | })); |
||
142 | domainTable = fillTable('node.domain', domainTable, domainDict.sort(function (a, b) { |
||
143 | return b[1] - a[1]; |
||
144 | })); |
||
145 | }; |
||
146 | |||
188 |
This check looks for functions where a
return
statement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBig
will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined
.This behaviour may not be what you had intended. In any case, you can add a
return undefined
to the other execution path to make the return value explicit.